package com.vast.ui.panel.button.views;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;

import com.vast.ui.component.socketinterface.protocol.VaSTUIComponentProtocol;
import com.vast.ui.component.socketinterface.request.Request;
import com.vast.ui.component.socketinterface.request.RequestQueue;
import com.vast.ui.component.state.Sequence;

public class ButtonSelectProvider implements SelectionListener {
	private final Image   on;
	private final Image   off;
	private final RequestQueue sendQueue;
	private final int requestSize = 20;
	private final Sequence sequence;
	public        int no;
	public        int value;
	
	public ButtonSelectProvider(Sequence  sequence, RequestQueue sendQueue, Image on, Image off) {
		this.sendQueue      = sendQueue;
		this.sequence       = sequence;
		this.on                    = on;
		this.off                   = off;
	}
	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
	

	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		Object object = e.getSource();
		if (object instanceof Button) {
			if(((Button)object).getSelection()) {
				((Button)object).setImage(on);
				value = 1;
			} else {
				((Button)object).setImage(off);
				value = 0;
			}
			no = Integer.valueOf(((Button)object).getToolTipText());
			if(sequence.isSendConnection()) {
				sendQueue.putRequest(createRequest());
			}
		}
	}

	public Request createRequest() {
		Request request = new Request(requestSize);
		request.writeInt(requestSize);
		request.command = VaSTUIComponentProtocol.PK_IO_UPDATE;
		request.writeInt(VaSTUIComponentProtocol.PK_IO_UPDATE);
		request.writeInt(no);
		request.writeInt(value);
		request.writeInt(VaSTUIComponentProtocol.PK_END_MARKER);
		return request;
	}
}
